#!/bin/sh

if [ -z $1 ];then
	uname1=$(uname)
	if [ 'Linux' = $uname1 ];then
      day=$(date -d "1 days ago" +%Y%m%d)
    else
	  day=$(date -v -1d +%Y%m%d)
    fi
else
  day=$1
fi

part_day=`date -d "${day}" +%Y%m01`
CUR_DIR=$(cd "$(dirname "$0")"; pwd)
UP_DIR=$(dirname $(dirname $(dirname $(dirname "$PWD"))))

source ${UP_DIR}/system.properties

sql=$(cat <<EOF
insert overwrite table ${hive_db}.dws_vehicle_hour_info_tmp select uid, tid, case when vehicle_hour_info.hour0 is null then 0 else 1 end, vehicle_hour_info.hour0.time hour0_time, vehicle_hour_info.hour0.mileage hour0_mileage, vehicle_hour_info.hour0.fuel hour0_fuel, case when vehicle_hour_info.hour1 is null then 0 else 1 end, vehicle_hour_info.hour1.time hour1_time, vehicle_hour_info.hour1.mileage hour1_mileage, vehicle_hour_info.hour1.fuel hour1_fuel, case when vehicle_hour_info.hour2 is null then 0 else 1 end, vehicle_hour_info.hour2.time hour2_time, vehicle_hour_info.hour2.mileage hour2_mileage, vehicle_hour_info.hour2.fuel hour2_fuel, case when vehicle_hour_info.hour3 is null then 0 else 1 end, vehicle_hour_info.hour3.time hour3_time, vehicle_hour_info.hour3.mileage hour3_mileage, vehicle_hour_info.hour3.fuel hour3_fuel, case when vehicle_hour_info.hour4 is null then 0 else 1 end, vehicle_hour_info.hour4.time hour4_time, vehicle_hour_info.hour4.mileage hour4_mileage, vehicle_hour_info.hour4.fuel hour4_fuel, case when vehicle_hour_info.hour5 is null then 0 else 1 end, vehicle_hour_info.hour5.time hour5_time, vehicle_hour_info.hour5.mileage hour5_mileage, vehicle_hour_info.hour5.fuel hour5_fuel, case when vehicle_hour_info.hour6 is null then 0 else 1 end, vehicle_hour_info.hour6.time hour6_time, vehicle_hour_info.hour6.mileage hour6_mileage, vehicle_hour_info.hour6.fuel hour6_fuel, case when vehicle_hour_info.hour7 is null then 0 else 1 end, vehicle_hour_info.hour7.time hour7_time, vehicle_hour_info.hour7.mileage hour7_mileage, vehicle_hour_info.hour7.fuel hour7_fuel, case when vehicle_hour_info.hour8 is null then 0 else 1 end, vehicle_hour_info.hour8.time hour8_time, vehicle_hour_info.hour8.mileage hour8_mileage, vehicle_hour_info.hour8.fuel hour8_fuel, case when vehicle_hour_info.hour9 is null then 0 else 1 end, vehicle_hour_info.hour9.time hour9_time, vehicle_hour_info.hour9.mileage hour9_mileage, vehicle_hour_info.hour9.fuel hour9_fuel, case when vehicle_hour_info.hour10 is null then 0 else 1 end, vehicle_hour_info.hour10.time hour10_time, vehicle_hour_info.hour10.mileage hour10_mileage, vehicle_hour_info.hour10.fuel hour10_fuel, case when vehicle_hour_info.hour11 is null then 0 else 1 end, vehicle_hour_info.hour11.time hour11_time, vehicle_hour_info.hour11.mileage hour11_mileage, vehicle_hour_info.hour11.fuel hour11_fuel, case when vehicle_hour_info.hour12 is null then 0 else 1 end, vehicle_hour_info.hour12.time hour12_time, vehicle_hour_info.hour12.mileage hour12_mileage, vehicle_hour_info.hour12.fuel hour12_fuel, case when vehicle_hour_info.hour13 is null then 0 else 1 end, vehicle_hour_info.hour13.time hour13_time, vehicle_hour_info.hour13.mileage hour13_mileage, vehicle_hour_info.hour13.fuel hour13_fuel, case when vehicle_hour_info.hour14 is null then 0 else 1 end, vehicle_hour_info.hour14.time hour14_time, vehicle_hour_info.hour14.mileage hour14_mileage, vehicle_hour_info.hour14.fuel hour14_fuel, case when vehicle_hour_info.hour15 is null then 0 else 1 end, vehicle_hour_info.hour15.time hour15_time, vehicle_hour_info.hour15.mileage hour15_mileage, vehicle_hour_info.hour15.fuel hour15_fuel, case when vehicle_hour_info.hour16 is null then 0 else 1 end, vehicle_hour_info.hour16.time hour16_time, vehicle_hour_info.hour16.mileage hour16_mileage, vehicle_hour_info.hour16.fuel hour16_fuel, case when vehicle_hour_info.hour17 is null then 0 else 1 end, vehicle_hour_info.hour17.time hour17_time, vehicle_hour_info.hour17.mileage hour17_mileage, vehicle_hour_info.hour17.fuel hour17_fuel, case when vehicle_hour_info.hour18 is null then 0 else 1 end, vehicle_hour_info.hour18.time hour18_time, vehicle_hour_info.hour18.mileage hour18_mileage, vehicle_hour_info.hour18.fuel hour18_fuel, case when vehicle_hour_info.hour19 is null then 0 else 1 end, vehicle_hour_info.hour19.time hour19_time, vehicle_hour_info.hour19.mileage hour19_mileage, vehicle_hour_info.hour19.fuel hour19_fuel, case when vehicle_hour_info.hour20 is null then 0 else 1 end, vehicle_hour_info.hour20.time hour20_time, vehicle_hour_info.hour20.mileage hour20_mileage, vehicle_hour_info.hour20.fuel hour20_fuel, case when vehicle_hour_info.hour21 is null then 0 else 1 end, vehicle_hour_info.hour21.time hour21_time, vehicle_hour_info.hour21.mileage hour21_mileage, vehicle_hour_info.hour21.fuel hour21_fuel, case when vehicle_hour_info.hour22 is null then 0 else 1 end, vehicle_hour_info.hour22.time hour22_time, vehicle_hour_info.hour22.mileage hour22_mileage, vehicle_hour_info.hour22.fuel hour22_fuel, case when vehicle_hour_info.hour23 is null then 0 else 1 end, vehicle_hour_info.hour23.time hour23_time, vehicle_hour_info.hour23.mileage hour23_mileage, vehicle_hour_info.hour23.fuel hour23_fuel,day from ${hive_db}.dws_vehicle_basic_info_pmi where day=${part_day};
EOF
)

filename=$2
opts=""
SQOOP_HOME="/usr/hdp/2.6.4.0-91/sqoop"
HIVE_EXEC='hive '
if [ -f "/opt/client/bigdata_env" ];then
    source /opt/client/bigdata_env
    opts="-D fs.s3a.access.key=S24JTQESPF5WIEOWZ4SK -D fs.s3a.secret.key=A8eOjlwNSNZNEvON1MxZQgLRNju4jdmYHddG2BZ3 -D fs.s3.endpoint=obs.cn-north-4.myhuaweicloud.com "
    SQOOP_HOME="/opt/sqoop/sqoop-1.4.7.bin__hadoop-2.6.0"
    HIVE_EXEC='beeline -u "jdbc:hive2://10.60.247.148:10000" '
fi

${HIVE_EXEC} --hiveconf "tez.queue.name=${queue_name}" -e "${sql}"

suffix=`date -d "${day}" +%y`
partition_value=$day
start=$(date +%s)
files=`ls ${CUR_DIR}/day_week_month_files/*/*.config`
if [[ ${filename} != "" ]];then
	files=`ls ${CUR_DIR}/$filename`
fi
for f in `ls ${files}`
do
source ${UP_DIR}/system.properties
source ${f}
type=`echo ${f} | awk -F '/' '{print $(NF-1)}'`
if [[ "$type" == "week" ]];then
        whichday=`date -d $day +%w`
        partition_value=`date -d "${day} -$[${whichday}-1] days" +%Y%m%d`
fi
if [[ "$type" == "month" ]];then
        partition_value=`date -d "${day}" +%Y%m01`
fi
if [[ "$type" == "year" ]];then
        partition_value=`date -d "${day}" +%Y0101`
fi
__hive_partition_key=" "
__hive_partition_value=" "
__mysql_table_name="${mysql_table_name} "

if [[ "${hive_partitioned_table}" = 1 ]];then
    __hive_partition_key="--hive-partition-key ${hive_partition_key} "
    __hive_partition_value="--hive-partition-value ${partition_value} "
fi

if [[ "${mysql_support_suffix}" = 1 ]];then
   __mysql_table_name="${mysql_table_name}_${suffix}"
fi

echo "开始导入${day}号${__mysql_table_name}分区为 ${__hive_partition_value} 的数据"
${SQOOP_HOME}/bin/sqoop export \
-D mapred.job.queue.name=${queue_name} -D sqoop.export.records.per.statement=1000 -D sqoop.export.statements.per.transaction=10 ${opts} \
--connect jdbc:mysql://${db_url}/${db_database}?useSSL=false \
--username ${db_username} \
--password ${db_pwd} \
--update-key ${mysql_update_key} \
--update-mode allowinsert \
--table ${__mysql_table_name} \
--fields-terminated-by '\t' \
--hcatalog-database ${hive_database} \
--hcatalog-table ${hive_table} ${__hive_partition_key} ${__hive_partition_value} \
--columns ${hive_columns}
    if [ $? -ne 0 ] ; then
        echo "${f}执行失败..."
    else
        echo "${f}执行成功..."
    fi
done
end=$(date +%s)
echo "ENDTIME：`date '+%Y%m%d-%H%M%S'`"
time=$(( $end - $start ))
echo "DURATION(S): $time"